<?php namespace Admin\Controller;
use Hdphp\Controller\Controller;
//定义商品类
class GoodsListController extends CommonController{
	private $model;
	//定义构造函数
	public function __init(){
		parent::__init();
		$this->model=new \Common\Model\GoodsList;

	}
	public function index(){
		//添加
		if(IS_POST){
			if(!$this->model->store()) View::error($this->model->getError());
			View::success('添加成功');
		}
		//分配数据
		$gid=Q('get.gid',0,'intval');
		View::with('gid',$gid);
		$type_tid=Q('get.type_tid',0,'intval');
		$goodsattr=new \Common\Model\Goodsattr;
		//关联数据表
		$data=$goodsattr->join('t_attr','t_attr_taid','=','taid')->where("goods_gid={$gid} and type=1")->get();
		$name=array();
		//组合数组
		foreach ($data as $k => $v) {
			$name[$v['taid']][]=$v;
		}
		foreach ($data as $v ){
			$newdata[$v['gaid']]=$v['gavalue'];
		}
//		p($name);
		View::with('name',$name);
		//从货品列表获得数据
		$goodslistData=$this->model->where("goods_gid=".Q('get.gid',0,'intval'))->get();
		
		foreach ($goodslistData as $k => $v) {
			$array=explode('|', $v['gattrid']);
			$arrays=array();
			$data2=array();
			foreach ($array as $v){
				$arrays[]=$v;
				$data2[]=$goodsattr->join('t_attr','t_attr_taid','=','taid')->where("gaid=$v")->pluck('gavalue');
			}
			$goodslistData[$k]['gattrid']=$arrays;
			$goodslistData[$k]['value']=$data2;
		}
		
	
		
		//p($goodslistData);
		View::with('goodslistData',$goodslistData);
		//载入模版
	    View::make();
	}
	public function edit(){
	    //添加
		if(IS_POST){
			if(!$this->model->edit()) View::error($this->model->getError());
			View::success('修改成功');
		}
		//分配数据
		$gid=Q('get.gid',0,'intval');
//		p(Q('get.'));
		View::with('gid',$gid);
		//实例化商品属性表
		$goodsattr=new \Common\Model\Goodsattr;
		//关联数据表
		$data=$goodsattr->join('t_attr','t_attr_taid','=','taid')->where("goods_gid={$gid} and type=1")->get();
		$name=array();
		//组合数组
		foreach ($data as $k => $v) {
			$name[$v['attname']][]=$v;
		}
		foreach ($data as $v ){
			$newdata[$v['gaid']]=$v['gavalue'];
		}
		//p($name);
		View::with('name',$name);
		View::with('newdata',$newdata);
		
		//从货品列表获得数据
		$goodslistData=$this->model->where("glid=".Q('get.glid',0,'intval'))->find();
		$array=explode('|', $goodslistData['gattrid']);
		//p($goodslistData);
		$arrays=array();
		foreach ($array as $v){
			$arrays[]=$newdata[$v];
		}
		$goodslistData['gattrid']=$arrays;
//		p($goodslistData);
		View::with('goodslistData',$goodslistData);
		//载入模版
	    View::make();
	}
	public function del(){
		$glid=Q('get.glid');
		$this->model->where("glid=$glid")->delete();
		View::success('删除成功');
	}




}
?>